package com.jdy.haoduoai.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.jdy.haoduoai.activity.BaseActivity.CancelLoadingDialogListener;
import com.jdy.haoduoai.util.CustomAsyncTask;
import com.ycsj.goldmedalnewconcept.R;

import rx.Subscription;

//import com.jdy.haoduoai.R;

/**
 * 
 * @author hufei
 * 
 */
public class LoadingDialog extends Dialog {
  private String strContent;

  private ImageView flydisk;
  private ImageView galaxy;
  private final Context mContext;
  private Animation mAnim;
  private View root_view;
  final int[] mPosition = new int[2];
  private int tPixel = 0;

  private CustomAsyncTask mCustomAsyncTask;
  private Subscription mSubscription;

  private ImageView mLoadingImg;

  private AnimationDrawable mAnim2;

  public void setAttachedSubscription(Subscription mSubscription) {
    this.mSubscription = mSubscription;
  }

  public void setAttachedAsyncTask(CustomAsyncTask aysc) {
    mCustomAsyncTask = aysc;
  }

  private CancelLoadingDialogListener mCancelLoadingDialogListener;

  public void setCancelLoadingDialogListener(
      CancelLoadingDialogListener mCancelLoadingDialogListener) {
    this.mCancelLoadingDialogListener = mCancelLoadingDialogListener;
  }

  private final Handler handler = new Handler() {
    @Override
    public void handleMessage(android.os.Message msg) {
      switch (msg.what) {
      case 2:
        // doScrollAction();
        break;
      default:
        break;
      }
    };
  };

  public LoadingDialog(Context context, int theme) {
    super(context, theme);
    mContext = context;
  }

  @Override
  public void cancel() {
    super.cancel();
    if (mCustomAsyncTask != null) {
      mCustomAsyncTask.cancel(false);
      mCustomAsyncTask = null;
    }

    if (mSubscription != null) {
      mSubscription.unsubscribe();
      mSubscription = null;
    }

    if (mCancelLoadingDialogListener != null) {
      mCancelLoadingDialogListener.onCancel();
    }
  }

  public LoadingDialog(Context context) {
    super(context);
    mContext = context;
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.dialog_loading_xxb);

    // if (LeXiaoXiaoBanApp.getInstance().getLoadingDlgBgWidth() <= 0) {
    // new Thread() {
    // @Override
    // public void run() {
    // Bitmap bt = BitmapFactory.decodeResource(mContext.getResources(),
    // R.drawable.loading_bg);
    //
    // if (bt != null) {
    // LeXiaoXiaoBanApp.getInstance().setLoadingDlgBgWidth(bt.getWidth());
    //
    // if (!bt.isRecycled()) {
    // bt.recycle();
    // }
    // bt = null;
    // }
    // };
    // }.start();
    // }

    mLoadingImg = (ImageView) findViewById(R.id.intermediate_loading_progress);

    mLoadingImg.setImageResource(R.drawable.anim_loading_3);

    mAnim2 = (AnimationDrawable) mLoadingImg.getDrawable();

    root_view = findViewById(R.id.root_view);
    flydisk = (ImageView) findViewById(R.id.img_flydisk);
    galaxy = (ImageView) findViewById(R.id.img_galaxy);
    galaxy.getLocationOnScreen(mPosition);

    mAnim = AnimationUtils.loadAnimation(mContext, R.anim.discover_anim);

    Window w = getWindow();
    WindowManager.LayoutParams lp = w.getAttributes();
    lp.gravity = Gravity.CENTER;
  }

  private void stopAnim() {
    // flydisk.clearAnimation();
    // galaxy.scrollTo(mPosition[0], mPosition[1]);
    // root_view.setVisibility(View.GONE);
    // handler.removeCallbacksAndMessages(null);
    if (mAnim2 != null && mAnim2.isRunning()) {
      mAnim2.stop();
    }

    if (mLoadingImg != null) {
      mLoadingImg.setVisibility(View.GONE);
    }
  }

  @Override
  public void onDetachedFromWindow() {
    strContent = null;
    stopAnim();
    super.onDetachedFromWindow();
  }

  @Override
  protected void onStop() {
    stopAnim();
    super.onStop();
  }

  @Override
  public void onAttachedToWindow() {
    android.widget.TextView tv = (android.widget.TextView) findViewById(R.id.loading_content);
    if (tv != null) {
      tv.setText(strContent);
    }

    super.onAttachedToWindow();
  }

  public void setDialogContent(String str) {
    strContent = str;
    android.widget.TextView tv = (android.widget.TextView) findViewById(R.id.loading_content);
    if (tv != null) {
      tv.setText(strContent);
    }
  }

  // private void startShowLoading() {
  // if (LeXiaoXiaoBanApp.getInstance().getLoadingDlgBgWidth() <= 0) {
  // new Thread() {
  // @Override
  // public void run() {
  // Bitmap bt = BitmapFactory.decodeResource(mContext.getResources(),
  // R.drawable.loading_bg);
  //
  // if (bt != null) {
  // LeXiaoXiaoBanApp.getInstance().setLoadingDlgBgWidth(bt.getWidth());
  // handler.sendEmptyMessage(2);
  //
  // if (!bt.isRecycled()) {
  // bt.recycle();
  // }
  // bt = null;
  // }
  // };
  // }.start();
  // } else {
  // doScrollAction();
  // }
  // }

  // private void doScrollAction() {
  // tPixel = 0;
  // galaxy.scrollTo(mPosition[0], mPosition[1]);
  // root_view.setVisibility(View.VISIBLE);
  //
  // flydisk.startAnimation(mAnim);
  //
  // handler.postDelayed(new Runnable() {
  // @Override
  // public void run() {
  // tPixel += 10;
  //
  // if (tPixel > ((LeXiaoXiaoBanApp.getInstance().getLoadingDlgBgWidth() -
  // Tools
  // .getPixelByDip(mContext, 130)))) {
  // tPixel = 0;
  // tPixel += 2;
  // galaxy.scrollTo(mPosition[0], mPosition[1]);
  // galaxy.scrollBy(10, 0);
  // } else {
  // galaxy.scrollBy(10, 0);
  // }
  //
  // handler.postDelayed(this, 40);
  // }
  // }, 40);
  // }

  @Override
  protected void onStart() {
    super.onStart();

    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        // startShowLoading();
        mLoadingImg.setVisibility(View.VISIBLE);
        mAnim2.start();
      }
    }, 200);

  }
}
